// components/date-range/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    ifRange: Boolean,
  },

  /**
   * 组件的初始数据
   */
  data: {
    monthList: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'],
    dayList: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'],
    beginList: [],
    endList: [],
    fromHour: '3',
    month: '',
    day: '',
    beginHour: '',
    endHour: '',
  },

  /**
   * 组件的方法列表
   */
  methods: {
    bindChange: function (e) {
      const val = e.detail.value;

      this.setData({
        month: this.data.monthList[val[0]],
        day: this.data.dayList[val[1]],
        beginHour: this.data.beginList[val[2]],
        endHour: this.data.endList[val[3]],
      });
      this.reSetList();
    },
    doNoThing(e) {
      console.log(this.data);
    },
    submit(e) {
      const date = new Date();
      const c_year = date.getFullYear();
      const c_month = date.getMonth() + 1;
      const c_day = date.getDate();
      const c_beginHour = date.getHours();
      const { year, month, day, beginHour, endHour, ifRange } = this.data;
      //处理未选择的情况
      let r_year = year;
      if(c_month>month){
        r_year = year+1
      }
      let r_endHour = endHour;
      if(endHour<beginHour){
        r_endHour = beginHour;
      }
      //处理提交时时间为选择前
      let err= false;
      if(r_year===year){
        if(month<c_month){
          err = true;
        }else if(month===c_month){
          if(day<c_day){
            err = true;
          }else if(day===c_day){
            if(beginHour<c_beginHour){
              err = true;
            }
          }
        }
      }
      if(err){
        wx.showToast({
          title: '选择的时间不符合要求',
          icon: 'none',
          duration: 2000
        })
      }else{
        let detail;
        if (ifRange) {
          detail = {
            beginDate: new Date(r_year, month - 1, day, beginHour),
            endDate: new Date(r_year, month - 1, day, r_endHour),
          }
        } else {
          detail = new Date(r_year, month - 1, day, beginHour);
        }
        this.triggerEvent('confirm', detail);
      }
    },
    reSetList: function (e) {
      const { month, day, beginHour } = this.data;
      const date = new Date();
      const c_year = date.getFullYear();
      const c_month = date.getMonth() + 1;
      const c_day = date.getDate();
      const c_beginHour = date.getHours();
      let monthList = [];
      let dayList = [];
      let beginList = [];
      let endList = [];
      let int_month = parseInt(month);
      if (month && c_month <= 10) {
        for (let i = c_month; i < c_month + 3; i++) {
          monthList.push(i);
        }
      } else if (c_month === 11) {
        monthList = [11, 12, 1];
      } else if (c_month === 12) {
        monthList = [12, 1, 2];
      }
      let day31 = "1,3,5,7,8,10,12";
      let day30 = "4,6,9,11";
      if (c_month === int_month) {
        //31天的月份
        if (day31.indexOf(c_month) > -1) {
          for (let i = c_day; i <= 31; i++) {
            dayList.push(i);
          }
          //30天的月份
        } else if (day30.indexOf(c_month) > -1) {
          for (let i = c_day; i <= 30; i++) {
            dayList.push(i);
          }
          //2月
        } else {
          if (c_year % 4 === 0 && (c_year % 100 != 0 || c_year % 400 == 0)) {
            for (let i = c_day; i <= 29; i++) {
              dayList.push(i);
            }
          } else {
            for (let i = c_day; i <= 28; i++) {
              dayList.push(i);
            }
          }
        }
      } else {
        //31天的月份
        if (day31.indexOf(int_month) > -1) {
          for (let i = 1; i <= 31; i++) {
            dayList.push(i);
          }
          //30天的月份
        } else if (day30.indexOf(int_month) > -1) {
          for (let i = 1; i <= 30; i++) {
            dayList.push(i);
          }
          //2月
        } else {
          if (c_year % 4 === 0 && (c_year % 100 != 0 || c_year % 400 == 0)) {
            for (let i = 1; i <= 29; i++) {
              dayList.push(i);
            }
          } else {
            for (let i = 1; i <= 28; i++) {
              dayList.push(i);
            }
          }
        }
      }
      if (c_month === int_month && c_day === day) {
        for (let i = c_beginHour; i <= 24; i++) {
          beginList.push(i);
        }
      } else {
        for (let i = 1; i <= 24; i++) {
          beginList.push(i);
        }
      }
      if (c_beginHour === beginHour) {
        for (let i = c_beginHour; i <= 24; i++) {
          endList.push(i);
        }
      } else {
        for (let i = beginHour; i <= 24; i++) {
          endList.push(i);
        }
      }
      this.setData({
        monthList,
        dayList,
        beginList,
        endList
      })

    },
    cancel() {
      this.triggerEvent('cancel');
    }
  },
  lifetimes: {
    ready() {
      const date = new Date();
      const year = date.getFullYear();
      const month = date.getMonth() + 1;
      const day = date.getDate();
      const beginHour = date.getHours();
      this.setData({
        year, month, day, beginHour,
      });
      this.reSetList();
    }
  }
})
